// pages/commentDetail/commentDetail.js
var requests = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentDetail: '', //评论的所有数据
    comment_id: '', //评论回复的id
    commentContent: '', //评论输入框双向绑定数据
    parent_id: '', //被回复id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(e) {
    console.log('跳转进来comment_id', e)
    let comment_id = e.comment_id
    if (typeof comment_id !== undefined) {
      let commentDetail = await this.getCommnetDetail(comment_id);
      console.log('评论详情', commentDetail)
      this.setData({
        commentDetail,
      })
      //获取表情
      let allMoods = await this.getMoods();
      console.log('获取的表情是', allMoods)
      if (allMoods) {
        this.setData({
          Moodarry: this.splitArray(allMoods, 24),
          comment_id
        });
      }

      wx.setNavigationBarTitle({
        title: '回复 ' + commentDetail.username + ' 的评论',
      });
    }

  },

  //提交评论 2022.11.1 Linyufan.com
  async submitComment() {
    wx.showLoading({
      title: '评论提交中...',
    })
    let request_data = {
      content: this.data.commentContent,
      check_code: 'fswh1543hfdf121hd',
      id: this.data.commentDetail.id,
      commentid: this.data.commentDetail.commentid,
      parent_id: this.data.commentDetail.parent_id,
      type: 'text', //voice,text //voice暂时不用了，只提交text的评论
    }
    console.log('提交的数据是>>', request_data);
    let url = ''
    let subCommRes = await requests.requestPostForComm(url,request_data);

    //这里看你的情况改造
    
    // requests.postAPI_submitCommentData(request_data, (returndata) => {
    //   console.log('回复评论的结果', returndata);
    // })

    console.log('回复评论的结果', subCommRes);
    wx.hideLoading()
    if (subCommRes.data.error_code !== '9002') {
      wx.showToast({
        title: subCommRes.data.error_description,
        icon: 'none',
        duration: 1500
      });
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }else {
      wx.showToast({
        title: subCommRes.data.error_description,
        icon: 'none',
        duration: 1500
      });
    }
    
  },


  // 绑定输入的评论 2022.11.1 Linyufan.com
  inputCommentContent(e) {
    this.setData({
      commentContent: e.detail.value,
    })
  },

  //选择表情到输入框 2022.11.1
  insertCommentContentMood: function (e) {
    let item = e.currentTarget.dataset.mood;
    console.log(this.data.commentContent);
    //这个if是解决输入框没内容，直接输入表情时出现 undefined 的问题
    if (this.data.commentContent && this.data.commentContent != undefined) {
      var commentContent = this.data.commentContent + item
    } else {
      var commentContent = item
    }
    this.setData({
      //commentcontent: this.data.commentcontent+=item,
      commentContent,
    });
    //console.log(commentcontent);
  },

  //获取评论详情的方法 2022.11.1 Linyufan.com
  async getCommnetDetail(commentid) {
    console.log('commentid', commentid)
    return new Promise((resolve) => {
      let request_data = {
        commentid,
      }
      requests.postAPI_commentDetailData(request_data, (returndata) => {
        resolve(returndata);
      })
    })
  },

  //获取表情方法 2022.11.1 Linyufan.com
  async getMoods() {
    return new Promise((resolve) => {
      let request_data = {}
      requests.postAPI_getMoodsData(request_data, (returndata) => {
        resolve(returndata);
      })
    })
  },

  //把从接口返回过来的表情数组进行分组，目前按24个为一组 2022.11.1 Linyufan.com
  splitArray: function (originArray, interval) {
    let newArray = []
    for (let i = 0, len = originArray.length; i < len; i += interval) {
      newArray.push(originArray.slice(i, i + interval))
    }
    return newArray
  },
  //把从接口返回过来的表情数组进行分组，目前按24个为一组 2022.11.1 Linyufan.com

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})